/*
 * @lc app=leetcode.cn id=219 lang=rust
 *
 * [219] 存在重复元素 II
 */

// @lc code=start
impl Solution {
    pub fn contains_nearby_duplicate(nums: Vec<i32>, k: i32) -> bool {
        let k = k + 1;
        if(nums.len() <= 1 || k <= 0)
        {
            return false;
        }
        
        use std::collections::HashSet;

        let mut set = HashSet::new();

        for i in 0..k{
            if(i < nums.len() as i32)
            {
                set.insert(&nums[i as usize]);
            }
        }

        if(k >= nums.len() as i32){
            if(set.len() < nums.len())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        if(k < nums.len() as i32)
        {
            for i in k..(nums.len() as i32){
                if(set.len() as i32 != k)
                {
                    return true;
                }
                set.remove(&nums[(i - k) as usize]);
                set.insert(&nums[i as usize]);
            }
        }
        
        if(set.len() as i32 != k){
            return true;
        }
        else
        {
            return false;
        }
    }
}
// @lc code=end

